package com.service.secondapp.service;

import com.huawei.paas.cse.tcc.annotation.TccTransaction;
import com.service.secondapp.bean.People;
import com.service.secondapp.bean.PeopleRowMapper;
import com.service.secondapp.controller.PeopleController;
import org.apache.servicecomb.provider.pojo.RpcSchema;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.Map;

/**
 * Created by wjh on 2018/3/27.
 */
@RestSchema(schemaId = "peopleService")
@RequestMapping(path = "/secondapp/people_service", produces = MediaType.APPLICATION_JSON)
public class PeopleServiceImpl implements  PeopleService{

    private static final Logger logger = LoggerFactory.getLogger(PeopleServiceImpl.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Value("${service_description.version}")
    public String version;

    /**
     * 获取people列表
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List getPeopleList(){
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from demo_people ");
        if (list.size() > 10) {
            return list.subList(0, 10);
        }
        return list;
    }

    /**
     * 通过peopleId获取用户属性
     * @param peopleId
     * @return
     */
    @RequestMapping(value = "/{people_id}", method = RequestMethod.GET)
    public People getPeopleById(@PathVariable(value = "people_id") Integer peopleId) {
        String sql = "select * from demo_people where people_id = ? ";
        People people = jdbcTemplate.queryForObject(sql, new PeopleRowMapper(), peopleId);
        return people;
    }

    /**
     * 新增people
     * @param people
     */
    @TccTransaction(confirmMethod = "confirm", cancelMethod = "cancel")
    @RequestMapping(method = RequestMethod.POST)
    public People addPeople(@RequestBody People people){
        return addPeople2Db(people);
    }

    @RequestMapping(value = "/not_tcc",method = RequestMethod.POST)
    public People addPeopleNoTcc(@RequestBody People people){
        return addPeople2Db(people);
    }

    private People addPeople2Db(People people) {
        String sql = "insert into demo_people values(?,?,?,?)";
        jdbcTemplate.update(sql,people.getPeopleId(),people.getPeopleName(),people.getStatus(),people.getDescription());
        return people;
    }

    public void confirm(People people) {
        logger.info("==========confirm==========");
        //confirm
        people.setStatus(1);
        updatePeople(people);
    }

    public void cancel(People people) {
        logger.info("==========cancel==========");
        //cancel
        people.setStatus(2);
        updatePeople(people);
    }

    /**
     * 更新用户状态
     * @param people
     */
    @RequestMapping(value="/{people_id}",method = RequestMethod.PUT)
    public People updatePeople(@RequestBody People people) {
        String sql = "update demo_people set status = " + people.getStatus() + " where people_id = ?";
        jdbcTemplate.update(sql, people.getPeopleId());
        return people;
    }

    /**
     * rpc、灰度测试
     * @param name
     * @return
     */
    @RequestMapping(value = "/rpc", method = RequestMethod.GET)
    @Override
    public String doRpcTest(@RequestParam(value = "name") String name) {
        return "rpc hello " + name + ",version:" + version;
    }
}
